<?php

namespace app\common\service\qrcode;

use app\common\model\settings\Setting as SettingModel;
use Grafika\Color;
use Grafika\Grafika;
use Endroid\QrCode\QrCode;

class CouponService extends Base
{
    // 商品信息
    private $coupon;

    // 用户id
    private $user_id;

    // 来源，微信小程序，公众号
    private $source;

    // 小程序码链接
    private $pages = [
        10 => 'pages/product/detail/detail'
    ];

    /**
     * 构造方法
     */
    public function __construct($coupon, $user, $source, $productType = 10)
    {
        parent::__construct();
        // 商品信息
        $this->coupon = $coupon;
        // 当前用户id
        $this->user_id = $user ? $user['user_id'] : 0;
        //来源
        $this->source = $source;
    }

    public function createQrCode($coupon_id, $user)
    {
        // 小程序id
        $appId = $this->coupon['app_id'];
        $qrcode = null;
        if ($this->source == 'wx') {
            // 小程序码参数
            $scene = "coupon_id:{$this->coupon['coupon_id']}";
            // 下载小程序码
            $qrcode = $this->saveQrcodeAli($appId, $scene, 'pages/index/coupon/receive');
        }
        return $qrcode;
    }

    public function createCouponQrCode($pid,$ptype=1)
    {
        // 小程序id
        $appId = $this->coupon['app_id'];
        $qrcode = null;
        if ($this->source == 'wx') {
            // 小程序码参数

            $scene = "coupon_id:{$this->coupon['coupon_id']}&uid:{$this->user_id}";
            // 下载小程序码
            if($ptype==1){
                $qrcode = $this->saveQrcodeAli($appId, $scene, 'pages/mine/coupon/receive');
            }else{
                $qrcode = $this->saveQrcodeAli($appId, $scene, 'pages/mine/coupon/receive');
            }
        } else if ($this->source == 'mp' || $this->source == 'h5') {
            $scene = "coupon_id:{$this->coupon['coupon_id']}&referee_id:" . ($this->user_id ?: '');
            $qrcode = new QrCode(base_url() . 'h5/pages/mine/coupon/receive?coupon_id=' . $this->coupon['coupon_id'] . '&app_id=' . $appId . '&referee_id=' . $this->user_id ?: '');
            $qrcode = $this->saveMpQrcode($qrcode, $appId, $scene, 'image_mp');
        }
        // 拼接海报图
        return $qrcode;
    }

}
